namespace Localisation.CustomProvider
{
    using System;
    using System.Web.Compilation;
    using System.Web.UI.Design;

    public sealed class CustomDesignTimeResourceProviderFactory : DesignTimeResourceProviderFactory
    {
        private CustomResourceProvider globalResourceProvider;
        private CustomResourceProvider localResourceProvider;
        private CustomDesignTimeResourceWriter localResourceWriter;

        /// <summary>
        /// Creates a global resource provider using the provided <see cref="T:System.IServiceProvider"></see> interface and resource class name.
        /// </summary>
        /// <param name="serviceProvider">A reference to the design host.</param>
        /// <param name="classKey">The name of the resource class.</param>
        /// <returns>
        /// Either an <see cref="T:System.Web.Compilation.IResourceProvider"></see> or an <see cref="T:System.Web.UI.Design.IDesignTimeResourceWriter"></see>.
        /// </returns>
        public override IResourceProvider CreateDesignTimeGlobalResourceProvider(IServiceProvider serviceProvider, string classKey)
        {
            // Return an IResourceProvider.
            if (globalResourceProvider == null)
            {
                globalResourceProvider = new CustomResourceProvider(null, classKey);
            }
            return globalResourceProvider;
        }

        /// <summary>
        /// Creates a local resource provider using the provided reference to the design host.
        /// </summary>
        /// <param name="serviceProvider">A reference to the design host.</param>
        /// <returns>
        /// An <see cref="T:System.Web.Compilation.IResourceProvider"></see> or a class derived from <see cref="T:System.Web.Compilation.IResourceProvider"></see>.
        /// </returns>
        public override IResourceProvider CreateDesignTimeLocalResourceProvider(IServiceProvider serviceProvider)
        {
            // Return an IResourceProvider.
            if (localResourceProvider == null)
            {
                localResourceProvider = new CustomResourceProvider(null, null);
            }
            return localResourceProvider;
        }

        /// <summary>
        /// Creates a local resource writer for using the provided reference to the design host.
        /// </summary>
        /// <param name="serviceProvider">A reference to the design host.</param>
        /// <returns>
        /// A <see cref="T:System.Resources.ResourceWriter"></see>.
        /// </returns>
        public override IDesignTimeResourceWriter CreateDesignTimeLocalResourceWriter(IServiceProvider serviceProvider)
        {
            // Return an IDesignTimeResourceWriter.
            if (localResourceWriter == null)
            {
                localResourceWriter = new CustomDesignTimeResourceWriter(serviceProvider.GetType().ToString());
            }
            return localResourceWriter;
        }
    }
}
